package com.example.xingzai.myapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import service.Myservice;


public class ServiceActivity extends AppCompatActivity implements View.OnClickListener {

    private Button button01, button02, button03, button04;
    private Mycon mycon;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);

        button01 = (Button) findViewById(R.id.start_service);
        button02 = (Button) findViewById(R.id.stop_service);
        button03 = (Button) findViewById(R.id.bind);
        button04 = (Button) findViewById(R.id.unbind);
        button01.setOnClickListener(this);
        button02.setOnClickListener(this);
        button03.setOnClickListener(this);
        button04.setOnClickListener(this);
        mycon = new Mycon();
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(ServiceActivity.this, Myservice.class);
        switch (v.getId()) {
            case R.id.start_service:
                ServiceActivity.this.startService(intent);
                break;
            case R.id.stop_service:
                ServiceActivity.this.stopService(intent);
                break;
            case R.id.bind:
                ServiceActivity.this.bindService(intent, mycon, BIND_AUTO_CREATE);
                break;
            case R.id.unbind:
                ServiceActivity.this.unbindService(mycon);
                break;
            default:
                break;
        }
    }

    class Mycon implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            ((Myservice.Mybinder) service).func();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}
